package com.example.spider.provider.m3u8;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 处理 m3u8 内容，初始化密钥，生产TS任务，合并任务
 *
 * @author lym
 */
@Slf4j
@Component
public class M3u8FileNameTemplate implements InitializingBean {

    @Value("${spider.m3u8.storage.dir:${user.home}/spider/m3u8}")
    private String m3u8StorageDir;

    @Value("${spider.m3u8.storage.taskFileName:m3u8Task.json}")
    private String taskFileName;

    @Value("${spider.m3u8.storage.spiderResultFileName:index.m3u8}")
    private String spiderResultFileName;

    @Value("${spider.m3u8.storage.parsedCacheFileName:m3u8MetaInfo.json}")
    private String parsedCacheFileName;

    @Value("${spider.m3u8.storage.tsPartDirName:ts}")
    private String tsPartDirName;

    @Value("${spider.m3u8.storage.mergedResulFileName:result.ts}")
    private String mergedResulFileName;

    @Value("${spider.m3u8.taskFileNameTemplate:{m3u8StorageDir}/{m3u8Id}/{taskFileName}}")
    private String taskFileNameTemplate = "{m3u8StorageDir}/{m3u8Id}/{taskFileName}";

    @Value("${spider.m3u8.metaFileNameTemplate:{m3u8StorageDir}/{m3u8Id}/{parsedCacheFileName}}")
    private String metaFileNameTemplate = "{m3u8StorageDir}/{m3u8Id}/{parsedCacheFileName}";

    @Value("${spider.m3u8.m3u8FileNameTemplate:{m3u8StorageDir}/{m3u8Id}/{spiderResultFileName}}")
    private String m3u8FileNameTemplate = "{m3u8StorageDir}/{m3u8Id}/{spiderResultFileName}";

    @Value("${spider.m3u8.partTsFileDirTemplate:{m3u8StorageDir}/{m3u8Id}/{tsPartDirName}}/")
    private String partTsFileDirTemplate = "{m3u8StorageDir}/{m3u8Id}/{tsPartDirName}/";

    @Value("${spider.m3u8.videoFileNameTemplate:{m3u8StorageDir}/{m3u8Id}/{mergedResulFileName}}")
    private String videoFileNameTemplate = "{m3u8StorageDir}/{m3u8Id}/{mergedResulFileName}";


    @Override
    public void afterPropertiesSet() throws Exception {
//        tsPartDirName = tsPartDirName.replace("\\", "/");
//        if(tsPartDirName.startsWith("/")) {
//            tsPartDirName = tsPartDirName.substring(1);
//        } if (!tsPartDirName.endsWith("/")) {
//            tsPartDirName = tsPartDirName + "/";
//        }
        taskFileNameTemplate = taskFileNameTemplate.replace("{m3u8StorageDir}", m3u8StorageDir).replace("{taskFileName}", taskFileName);
        m3u8FileNameTemplate = m3u8FileNameTemplate.replace("{m3u8StorageDir}", m3u8StorageDir).replace("{spiderResultFileName}", spiderResultFileName);
        metaFileNameTemplate = metaFileNameTemplate.replace("{m3u8StorageDir}", m3u8StorageDir).replace("{parsedCacheFileName}", parsedCacheFileName);
        partTsFileDirTemplate = partTsFileDirTemplate.replace("{m3u8StorageDir}", m3u8StorageDir).replace("{tsPartDirName}", tsPartDirName);
        videoFileNameTemplate = videoFileNameTemplate.replace("{m3u8StorageDir}", m3u8StorageDir).replace("{mergedResulFileName}", mergedResulFileName);
    }

    public String getM3u8StorageDir() {
        return m3u8StorageDir;
    }

    public String calculateTaskFileName() {
        return taskFileName;
    }

    public String getSpiderResultFileName() {
        return spiderResultFileName;
    }

    public String getParsedCacheFileName() {
        return parsedCacheFileName;
    }

    public String getTsPartDirName() {
        return tsPartDirName;
    }

    public String getMergedResulFileName() {
        return mergedResulFileName;
    }

    public String calculateVideoFileName(String id) {
        return videoFileNameTemplate.replace("{m3u8Id}", id);
    }

    public String calculateTsPartFileDir(String id) {
        return partTsFileDirTemplate.replace("{m3u8Id}", id);
    }

    public String calculateMetaFileName(String id) {
        return metaFileNameTemplate.replace("{m3u8Id}", id);
    }

    public String calculateM3u8FileName(String id) {
        return m3u8FileNameTemplate.replace("{m3u8Id}", id);
    }

    public String calculateTaskFileName(String id) {
        return taskFileNameTemplate.replace("{m3u8Id}", id);
    }

}
